﻿using System.Linq;
using System.Web.Mvc;
using ASPItalia.ModelVirtualCasting.Web.Filters;
using ASPItalia.ModelVirtualCasting.Web.Models;
using ASPItalia.ModelVirtualCasting.Common;
using System;

namespace ASPItalia.ModelVirtualCasting.Web.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        #region refactoring per testability
        private IModelRepository modelRepository;
        private INewsRepository newsRepository;

        internal HomeController(IModelRepository modelRepository, INewsRepository newsRepository)
        {
            if (modelRepository == null)
                throw new ArgumentNullException("modelRepository");
            if (newsRepository == null)
                throw new ArgumentNullException("newsRepository");

            this.modelRepository = modelRepository;
            this.newsRepository = newsRepository;
        }

        public HomeController()
            : this(ApplicationContext.Current.Models, 
            ApplicationContext.Current.News)
        { }
        #endregion
        
        #region oldIndex
        public ActionResult Index()
		{
            HomepageViewModel viewModel = new HomepageViewModel();

            viewModel.Models.AddRange(modelRepository.GetRandomModels(5));
            viewModel.News.AddRange(newsRepository.GetLastNewsDTO(3));
        	
            return View(viewModel);

		}
        #endregion

        #region Better Index
        //[ShowNews(Number = 3)]
        //[ShowModels(Number = 5)]
        //public ActionResult Index()
        //{
        //    this.ViewData.Model = new HomepageViewModel();
        //    return View();
        //}
        #endregion
    }
}
